{% extends 'posts.html' %}
{% block content %}
    <div class="layui-col-md8 content detail">
        <div class="fly-panel detail-box">
            <h1>{{ post.title }}</h1>
            <div class="fly-detail-info">
                <!-- <span class="layui-badge">审核中</span> -->
                <span class="layui-badge layui-bg-green fly-detail-column">{{ post.catalog.name }}</span>

                {% if post.is_closed %}
                    <span class="layui-badge" style="background-color: #5FB878;">已结</span>
                {% else %}
                    <span class="layui-badge" style="background-color: #999;">未结</span>
                {% endif %}

                {% if post.is_top %}
                    <span class="layui-badge layui-bg-black">置顶</span>
                {% endif %}
                {% if post.is_cream %}
                    <span class="layui-badge layui-bg-red">精帖</span>
                {% endif %}
                <div class="fly-admin-box" data-id="{{ post.pk }}">
                    {% if post.creator == user_info or request.user.is_superuser or post.catalog.moderator == user_info %}

                        <span class="layui-btn layui-btn-xs jie-admin" type="del">删除</span>
                        {% if not post.is_closed %}
                            <span class="layui-btn layui-btn-xs jie-admin" type="set" field="closed"
                                  rank="1">结帖</span>
                        {% endif %}

                        {% if request.user.is_superuser %}

                            {% if post.is_top %}
                                <span class="layui-btn layui-btn-xs jie-admin" type="set" field="top" rank="0"
                                      style="background-color:#ccc;">取消置顶</span>
                            {% else %}
                                <span class="layui-btn layui-btn-xs jie-admin" type="set" field="top"
                                      rank="1">置顶</span>
                            {% endif %}
                        {% endif %}

                        {% if request.user.is_superuser or post.catalog.moderator == user_info %}
                            {% if post.is_cream %}
                                <span class="layui-btn layui-btn-xs jie-admin" type="set" field="cream" rank="0"
                                      style="background-color:#ccc;">取消加精</span>
                            {% else %}
                                <span class="layui-btn layui-btn-xs jie-admin" type="set" field="cream"
                                      rank="1">加精</span>
                            {% endif %}
                        {% endif %}


                    {% endif %}
                </div>

                <span class="fly-list-nums">
            <a href="#comment"><i class="iconfont" title="回答">&#xe60c;</i> {{ post.comment_count|default:0 }}</a>
            <i class="iconfont" title="人气"{% if user_info in post.viewed.all %} style="color: #ff983e;"{% endif %}>&#xe60b;</i> {{ post.view_count|default:0 }}
        </span>
            </div>
            <div class="detail-about">
                <a class="fly-avatar" href="/user/{{ post.creator.pk }}">
                    <img src="{{ post.creator.avatar.url }}" alt="{{ post.creator.user.username }}">
                </a>
                <div class="fly-detail-user">
                    <a href="/user/{{ post.creator.pk }}" class="fly-link">
                        <cite>{{ post.creator.user.username|default:'无名' }}</cite>
                        {% if post.creator.renzheng %}
                            <i class="iconfont icon-renzheng">{{ post.creator.renzheng }}</i>
                        {% endif %}
                        {% if post.creator.vip|default:0 > 0 %}
                            <i class="layui-badge fly-badge-vip">VIP{{ post.creator.vip }}</i>
                        {% endif %}
                    </a>
                    <span>{{ post.create_at|date:'Y-m-d H:i' }}</span>
                </div>
                <div class="detail-hits" id="LAY_jieAdmin" data-id="{{ post.pk }}">
                    <span style="padding-right: 10px; color: #FF7200">悬赏：{{ post.reward|default:0 }}金币</span>
                    {% if request.user.is_authenticated and not post.is_closed %}
                        {% if post.creator == user_info or request.user.is_superuser %}
                            <span class="layui-btn layui-btn-xs jie-admin" type="edit"><a
                                    href="{% url 'edit_post' post.pk %}">编辑此贴</a></span>
                        {% endif %}
                    {% endif %}
                </div>
            </div>
            <div class="detail-body photos">
                {{ post.content }}
            </div>
        </div>

        <div class="fly-panel detail-box" id="flyReply">
            <fieldset class="layui-elem-field layui-field-title" style="text-align: center;">
                <legend>回帖</legend>
            </fieldset>

            <ul class="jieda" id="jieda">
                {% for item in comments %}
                    <li data-id="{{ item.pk }}" {% if item.is_adopted %}class=jieda-daan{% endif %}>
                        <a name="item-{{ item.pk }}"></a>
                        <div class="detail-about detail-about-reply">
                            <a class="fly-avatar" href="/user/{{ item.user.pk }}">
                                <img src="{{ item.user.avatar.url }}" alt=" ">
                            </a>
                            <div class="fly-detail-user">
                                <a href="/user/{{ item.user.pk }}" class="fly-link">
                                    <cite>{{ item.user.user.username }}</cite>
                                    {% if item.user.renzheng %}
                                        <i class="iconfont icon-renzheng">{{ item.user.renzheng }}</i>
                                    {% endif %}
                                    {% if item.user.vip|default:0 > 0 %}
                                        <i class="layui-badge fly-badge-vip">VIP{{ item.user.vip }}</i>
                                    {% endif %}
                                </a>

                                {% if post.creator == item.user %}
                                    <span>(楼主)</span>
                                {% endif %}
                                {% if item.user.user.is_superuser %}
                                    <span style="color:#5FB878">(管理员)</span>
                                {% endif %}
                                {% if not item.user.is_active %}
                                    <span style="color:#999">（该号已被封）</span>
                                {% endif %}
                            </div>

                            <div class="detail-hits">
                                <span>{{ item.create_at|date:'Y-m-d H:i' }}</span>
                            </div>
                            {% if item.is_adopted %}
                                <i class="iconfont icon-caina" title="最佳答案"></i>
                            {% endif %}
                        </div>
                        <div class="detail-body jieda-body photos">
                            {{ item.content }}
                        </div>
                        <div class="jieda-reply">
                            {% if request.user.is_active %}
                                <span class="jieda-zan {% if user_info in item.likers.all %}zanok{% endif %}"
                                      type="zan">
                  <i class="iconfont icon-zan"></i>
                  <em>{{ item.like_count|default:0 }}</em>
                </span>
                                <span type="reply">
                  <i class="iconfont icon-svgmoban53"></i>
                  回复
                </span>
                            {% endif %}



                            <div class="jieda-admin">
                                {% if item.user == user_info and not post.is_closed %}
                                    <span type="edit">编辑</span>
                                {% endif %}
                                {% if request.user.is_superuser %}
                                    <span type="del">删除</span>
                                {% endif %}
                                {% if not post.accepted and request.user.is_authenticated  and post.creator == user_info %}
                                    <span class="jieda-accept" type="accept">采纳</span>
                                {% endif %}

                            </div>

                        </div>
                    </li>
                    {% empty %}
                    <li class="fly-none">消灭零回复</li>
                {% endfor %}

            </ul>
{#            {% if comments|length > 1 %}#}
{#                {% set url_prefix=("/post/" + post.pk|string) %}#}
{##}
{#                {{ pagination.paginate(comment_page, url_prefix) }}#}
{#            {% endif %}#}

            {% if request.user.is_authenticated %}
                {% if not post.is_closed %}
                    <div class="layui-form layui-form-pane">
                        <form action="/comment/add/" method="post">
                            <div class="layui-form-item layui-form-text">
                                <a name="comment"></a>
                                <div class="layui-input-block">
              <textarea id="L_content" name="content" required lay-verify="required" placeholder="请输入内容"
                        class="layui-textarea fly-editor" style="height: 150px;"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                {% csrf_token %}
                                <input type="hidden" name="id" value="{{ post.pk }}">
                                <button class="layui-btn" lay-filter="*" lay-submit>提交回复</button>
                            </div>
                        </form>
                    </div>
                {% else %}
                    <div class="layui-form-pane">
                        <p style="text-align: center">此帖已结</p>
                    </div>
                {% endif %}
            {% else %}
                <a class="layui-btn" href="{% url 'login' %}?next={% url 'post_detail' post.pk %}" style="text-align: center">登录后回贴</a>
            {% endif %}


        </div>
    </div>
{% endblock %}
{% block fly_use %}
    .use(['fly', 'face'], function(){
    var $ = layui.$
    ,fly = layui.fly;
    $('.detail-body').each(function(){
    var othis = $(this), html = othis.html();
    othis.html(fly.content(html));
    });
    });
{% endblock %}
{% block other_js %}
<script>
    const csrftoken = '{{ csrf_token }}';
</script>
{% endblock %}